package com.yozinga;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.yozinga.util.AppData;

public class BaseActivity extends FragmentActivity implements OnClickListener{


	String topActivity;
	
	
	
	@Override
	protected void onStart() {
		super.onStart();
		//topActivity = getCurrentTopActivity();
		//Log.i("Today","Top Activity is"+topActivity);
		topActivity = this.getClass().getSimpleName();
		Log.i("Today","onResume Top Activity is"+topActivity);
		if(!topActivity.equalsIgnoreCase("OfferDealPage")&&!topActivity.equalsIgnoreCase("com.laundrylocker.barcodescanner.CaptureActivity")){
			Button favourites = (Button) findViewById(R.id.favorites);
			if(favourites!=null){
				favourites.setOnClickListener(this);
			}
			Button recommend = (Button) findViewById(R.id.recommend);
			if(recommend!=null){
				recommend.setOnClickListener(this);
			}
			
			Button redeemed = (Button) findViewById(R.id.redeemed);
			if(redeemed!=null){
				redeemed.setOnClickListener(this);
			}
			
			Button settings = (Button) findViewById(R.id.settings);
			if(settings!=null){
				settings.setOnClickListener(this);
			}
			
			/*((Button) findViewById(R.id.favorites)).setOnClickListener(this);
			((Button) findViewById(R.id.recommend)).setOnClickListener(this);
			((Button) findViewById(R.id.redeemed)).setOnClickListener(this);
			((Button) findViewById(R.id.settings)).setOnClickListener(this);*/
		}
		((ImageView) findViewById(R.id.ivBackButton)).setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		topActivity = this.getClass().getSimpleName();
		Log.i("Today","onResume Top Activity is"+topActivity);
	}

	
	@Override
	public void onClick(View v) {
		Intent tabIntent = null;
		if(v.getId() == R.id.settings){
			if(!topActivity.equals("SettingsActivity")){
				tabIntent = new Intent(this,SettingsActivity.class);
				startActivity(tabIntent);
			}
		}else if(v.getId() == R.id.recommend){
			if(!topActivity.equals("Recommended")){
				tabIntent = new Intent(this,Recommended.class);
				startActivity(tabIntent);
			}
		}else if(v.getId() == R.id.redeemed){
			if(!topActivity.equals("Redeemed")){
				tabIntent = new Intent(this,Redeemed.class);
				startActivity(tabIntent);
			}
		}else if(v.getId() == R.id.favorites){
			if(!topActivity.equals("Favourites")){
				tabIntent = new Intent(this,Favourites.class);
				startActivity(tabIntent);
			}
		}else if(v.getId() == R.id.ivBackButton){
			finish();
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) 
	{
		super.onSaveInstanceState(outState);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) 
	{
		super.onRestoreInstanceState(savedInstanceState);
		if(AppData.TEMP_VALUE == null)
		{
			Intent i=new Intent(getApplicationContext(),HomeActivity.class);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			finish();
		}

	}
	
	
	/**
	 * Retrieve the TopActivity Package Name and Compare with Application Package Name.
	 * 
	 * @param context
	 * @return
	 */
	/*public String getCurrentTopActivity() {
		ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
		ComponentName topActivity = null ;
		if (!tasks.isEmpty()) {
			topActivity = tasks.get(0).topActivity;
		}
		return topActivity.getClassName().toString();
	}*/
}